import { XlsxType } from "../tools/Define";

export default class XlsxData{

    private data:[] = null;

    private keys:{} = null;

    private index:{} = null;

    private type:number = XlsxType.DATA;

    constructor(json) {
        if (json) {
           this.initWithJson(json);
        }
    }

    initWithJson(json){
        this.data = json.data
        this.type = json.type;
        if(this.type == XlsxType.DATA){
            this.keys = json.key;
            this.index = json.index;
        }else{
            
        } 
    }

    size():number{
        return this.data.length;
    }

    getData():[]{
        return this.data;
    }

    getKeys():{}{
        return this.keys;
    }

    // forEach(func:(key,value)=>void){
    //     for (const key in this.keys) {
    //         if (this.keys.hasOwnProperty(key)) {
    //             const index = this.keys[key];
    //             cc.log(' this.data[key][index] ',this.data,index,key)
    //             func(key,this.data[key][index])
    //         }
    //     }
    // }
    forEach(func: (key: string | number, value: any[]) => void) {
        for (const key in this.keys) {
            if (this.keys.hasOwnProperty(key)) {
                const element = this.keys[key];
                func(key, this.data[element])
            }
        }
    }
    getRowData(ID:any){
        ID = "" + ID;
        let data =  this.data[this.keys[ID]];
        if(data == undefined){
            cc.warn(' getRowData is null ',ID)
        }
        return data;
    }

    /**
     * 
     * @param ID 行ID
     * @param index 列索引位置
     */
    getValue(ID:any,index?:number){
        ID = "" + ID;
        if(this.type == XlsxType.DATA  && index != undefined){
            return this.data[this.keys[ID]][index];
        }else{
            return this.data[ID];
        }
    }

    /**
     * 拷贝数值 必须是数组类型
     * @param ID 
     * @param index 
     */
    copyValue(ID:any,index?:number){
        let list = [];
        let value:any[] = this.getValue(ID,index);
        if(value){
            for (let index = 0; index < value.length; index++) {
                const element = value[index];
                list.push(element)
            }
        }
        return list;
    }

    getIndex(lineID:number):[]{
        return this.index[lineID];
    }

    copyIndex(lineID:number):any[]{
        let temp:any[] = []
        let list = this.getIndex(lineID);
        if(list){
            for (let index = 0; index < list.length; index++) {
                const element = list[index];
                temp.push(element)
            }
        }
        return temp;
    }


}
